package com.example.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.enums.ResultCodeEnum;
import com.example.entity.Account;
import com.example.entity.Playlist;
import com.example.exception.CustomException;
import com.example.mapper.PlaylistMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 歌单信息表业务处理
 **/
@Service
public class PlaylistService {

    @Resource
    private PlaylistMapper playlistMapper;

    /**
     * 新增
     */
    public void add(Playlist playlist) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (ObjectUtil.isNull(currentUser)) {
            throw new CustomException(ResultCodeEnum.TOKEN_CHECK_ERROR);
        }
        playlist.setUserId(currentUser.getId());
        playlist.setTime(DateUtil.now());
        playlistMapper.insert(playlist);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        playlistMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            playlistMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Playlist playlist) {
        playlistMapper.updateById(playlist);
    }

    /**
     * 根据ID查询
     */
    public Playlist selectById(Integer id) {
        return playlistMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Playlist> selectAll(Playlist playlist) {
        return playlistMapper.selectAll(playlist);
    }

    /**
     * 分页查询
     */
    public PageInfo<Playlist> selectPage(Playlist playlist, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Playlist> list = playlistMapper.selectAll(playlist);
        return PageInfo.of(list);
    }

    public List<Playlist> selectTop(Integer num) {
        return playlistMapper.selectTop(num);
    }

}